---
title: "merging_sript_senders_receivers"
output: pdf_document
date: '2023-01-03'
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE,message=FALSE)
setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata")
```

#RECEIVERS 

```{r}
rm(list=ls())

```

```{r}
setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata")


df_receivers <- read.csv("receivers_1_working_wide_v2.csv") 
```



```{r}


df2 <- subset(df_receivers, select = c(respondent, respid_env, respid_imm,
                             respid_trans, post_env, post_imm,
                            post_trans, pre_env, pre_imm, 
                            pre_trans, argueside_env, argueside_imm,
                            argueside_trans, issueside_env, issueside_imm,
                            issueside_trans, quarkelt1_env, quarkelt1_imm, 
                            quarkelt1_trans, quarkelt2_env, quarkelt2_imm, 
                            quarkelt2_trans, persuasive_env, persuasive_imm, 
                            persuasive_trans, clarity_env, clarity_imm, 
                            clarity_trans, reasoning_env, reasoning_imm,
                            reasoning_trans, binary_env, binary_imm, 
                            binary_trans, movement_trans, movement_imm, movement_env)) %>% 
  mutate_at(vars(respondent, respid_env, respid_imm,
                             respid_trans, post_env, post_imm,
                            post_trans, pre_env, pre_imm, 
                            pre_trans,argueside_env, argueside_imm,
                            argueside_trans, issueside_env, issueside_imm,
                            issueside_trans, quarkelt1_env, quarkelt1_imm, 
                            quarkelt1_trans, quarkelt2_env, quarkelt2_imm, 
                            quarkelt2_trans, persuasive_env, persuasive_imm, 
                            persuasive_trans, clarity_env, clarity_imm, 
                            clarity_trans, reasoning_env, reasoning_imm,
                            reasoning_trans, binary_env, binary_imm, 
                            binary_trans, movement_trans, movement_imm, movement_env), .funs = as.character)


```


```{r}
library(dplyr)
library(tidyr) 
df_long <- df2 %>% 
  pivot_longer(-respondent, names_sep = '_', names_to = c('variable', 'issue')) %>%
  pivot_wider(names_from = 'variable', values_from = 'value')

# Exclude respid's with no information
df_long <- df_long %>% 
  filter(respid != "")

```

```{r}

# Specify the folder path
folder_path <- "/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata/"

# Create the full file path including the filename and extension 
file_path <- file.path(folder_path, "receivers_1_working_long.csv")

# Export the dataframe to CSV 
write.csv(df_long, file = file_path, row.names = FALSE)

# Check if file exists 
file.exists(file_path) 

```

#SENDERS

```{r}
setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata")

df_senders <- read.csv("senders_working2_v3.csv") 

df_senders <- subset(df_senders, write_yes!=FALSE) #Removing participants in the Sender survey who chose not to write an argument  

```


#MERGING
```{r}
persuasion_merged <- as.data.frame(dplyr::inner_join(df_long, df_senders, by = "respid"))


```

```{r}

# Specify the folder path
folder_path <- "/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata/"


# Create the full file path including the filename and extension 
file_path <- file.path(folder_path, "working_data_final_sr1_v2.csv")

# Export the dataframe to CSV 
write.csv(persuasion_merged, file = file_path, row.names = FALSE)

# Check if file exists 
file.exists(file_path) 

```

